package summary;

import java.util.*;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 9:30
 * https://leetcode-cn.com/problems/intersection-of-two-arrays/
 */
public class Title349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        int[] result = new int[nums1.length + nums2.length];
        for (int i = 0; i < nums1.length; i++) {
            set1.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            set2.add(nums2[i]);
        }
        int count = 0;
        for (Integer integer : set1) {
            if (set2.contains(integer)) {
                result[count++] = integer;
            }
        }
        return Arrays.copyOfRange(result, 0, count);
    }


    public int[] intersection1(int[] nums1, int[] nums2) {
        Set<Integer> hash = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            hash.add(nums1[i]);
        }
        Set<Integer> result = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if (hash.contains(nums2[i])) {
                result.add(nums2[i]);
            }
        }
        int[] arr = new int[result.size()];
        int index = 0;
        for (Integer i : result) {
            arr[index++] = i;
        }
        return arr;
    }


    public static void main(String[] args) {

    }
}
